/*
 * Created by JFormDesigner on Wed Oct 27 12:58:39 CST 2021
 */

package view.CURD;

import library.controller.FrameListener;
import library.po.Book;
import library.po.BookType;
import library.service.impl.BookTypeServiceImpl;
import library.service.BookTypeService;

import java.awt.*;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class BookDialog extends JDialog {
    public BookDialog(BookViewDialog owner,String title,Book book) {
        super(owner);
        this.book = book;
        this.setTitle(title);
        this.listener = owner.getListener();
        this.setVisible(true);
        initComponents();
    }

    private void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        bookName = new JLabel();
        ISBN = new JLabel();
        writer = new JLabel();
        reserve = new JLabel();
        type = new JLabel();
        nameValue = new JTextField();
        ISBNValue = new JTextField();
        writerValue = new JTextField();
        typeValue = new JComboBox();
        reserveValue = new JTextField();
        okButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(
                    new javax.swing.border.EmptyBorder(0, 0, 0, 0), ""
                    , javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BOTTOM
                    , new java.awt.Font("D\u0069al\u006fg", java.awt.Font.BOLD, 12)
                    , java.awt.Color.red), dialogPane.getBorder()));
            dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.beans.PropertyChangeEvent e
                ) {
                    if ("\u0062or\u0064er".equals(e.getPropertyName())) throw new RuntimeException()
                            ;
                }
            });
            dialogPane.setLayout(null);

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- bookName ----
                bookName.setText("\u4e66  \u540d\uff1a");
                bookName.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 16));
                contentPanel.add(bookName);
                bookName.setBounds(new Rectangle(new Point(25, 40), bookName.getPreferredSize()));

                //---- ISBN ----
                ISBN.setText("ISBN:");
                ISBN.setFont(new Font("Lucida Console", ISBN.getFont().getStyle(), ISBN.getFont().getSize() + 4));
                contentPanel.add(ISBN);
                ISBN.setBounds(30, 90, ISBN.getPreferredSize().width, 24);

                //---- writer ----
                writer.setText("\u4f5c  \u8005\uff1a");
                writer.setFont(new Font("\u5e7c\u5706", writer.getFont().getStyle() & ~Font.BOLD, writer.getFont().getSize() + 4));
                contentPanel.add(writer);
                writer.setBounds(new Rectangle(new Point(30, 145), writer.getPreferredSize()));

                //---- reserve ----
                reserve.setText("\u8fdb\u4e66\u91cf\uff1a");
                reserve.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 16));
                contentPanel.add(reserve);
                reserve.setBounds(new Rectangle(new Point(30, 255), reserve.getPreferredSize()));

                //---- type ----
                type.setText("\u7c7b  \u522b\uff1a");
                type.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 16));
                contentPanel.add(type);
                type.setBounds(new Rectangle(new Point(30, 200), type.getPreferredSize()));
                contentPanel.add(nameValue);
                nameValue.setBounds(95, 40, 135, nameValue.getPreferredSize().height);
                contentPanel.add(ISBNValue);
                ISBNValue.setBounds(95, 90, 135, 22);
                contentPanel.add(writerValue);
                writerValue.setBounds(95, 145, 135, 22);
                bookTypeList = bookTypeService.selectAllBookType();
                Iterator<BookType> iter = bookTypeList.iterator();
                while (iter.hasNext()){
                    BookType bookType = iter.next();
                    typeValue.addItem(bookType.getBookTypeName());
                }
                contentPanel.add(typeValue);
                typeValue.setBounds(95, 195, 135, typeValue.getPreferredSize().height);
                contentPanel.add(reserveValue);
                reserveValue.setBounds(95, 255, 135, 22);

                //---- okButton ----
                okButton.setText("确认");
                okButton.addActionListener(listener);
                okButton.setActionCommand("BookPost");
                contentPanel.add(okButton);
                okButton.setBounds(80, 310, 110, 35);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel);
            contentPanel.setBounds(10, 15, 278, 375);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < dialogPane.getComponentCount(); i++) {
                    Rectangle bounds = dialogPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialogPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialogPane.setMinimumSize(preferredSize);
                dialogPane.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(305, 430);
        setLocationRelativeTo(getOwner());
        if ("添加图书".equals(this.getTitle())){

        }
        else if ("修改图书信息".equals(this.getTitle())){
            ISBNValue.setText(book.getBookIsbn().toString());
            ISBNValue.setEditable(false);
            nameValue.setText(book.getBookName());
            nameValue.setEditable(false);
            writerValue.setEditable(false);
            reserveValue.setText(book.getBookReserve().toString());
            writerValue.setText(book.getBookWriter());
        }
    }
    public Book getBookInstance(){
        Book toolBook = new Book();
        if (!this.ISBNValue.getText().trim().equals("") && this.ISBNValue.getText().trim()!=null){
            System.out.println(this.ISBNValue.getText().trim());
            toolBook.setBookIsbn(Integer.parseInt(this.ISBNValue.getText().trim()));}
        else
            JOptionPane.showMessageDialog(this,"ISBN不可为空！","错误",JOptionPane.ERROR_MESSAGE);
        if (!this.nameValue.getText().trim().equals("")&&this.nameValue.getText().trim()!=null)
            toolBook.setBookName(this.nameValue.getText().trim());
        else
            JOptionPane.showMessageDialog(this,"书名不可为空！","错误",JOptionPane.ERROR_MESSAGE);
        if (!this.reserveValue.getText().trim().equals("")&&this.reserveValue.getText().trim()!=null)
            toolBook.setBookReserve(Integer.parseInt(this.reserveValue.getText().trim()));
        else
            JOptionPane.showMessageDialog(this,"进书量不可为空！","错误",JOptionPane.ERROR_MESSAGE);
        if (this.book!=null)
            toolBook.setBookState(book.getBookState());
        else
            toolBook.setBookState(1);
        toolBook.setBookType(bookTypeService.selectByTypeName(this.typeValue.getSelectedItem().toString().trim()).getBookTypeId());
        if (!this.writerValue.getText().trim().equals("")&&this.writerValue.getText().trim()!=null)
            toolBook.setBookWriter(this.writerValue.getText().trim());
        else
            JOptionPane.showMessageDialog(this,"作者不可为空！","错误",JOptionPane.ERROR_MESSAGE);
        return toolBook;
    }
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel bookName;
    private JLabel ISBN;
    private JLabel writer;
    private JLabel reserve;
    private JLabel type;
    private JTextField nameValue;
    private JTextField ISBNValue;
    private JTextField writerValue;
    private JComboBox typeValue;
    private JTextField reserveValue;
    private JButton okButton;
    private FrameListener listener;
    private Book book;
    private BookTypeService bookTypeService = new BookTypeServiceImpl();
    private List<BookType> bookTypeList;
}
